package com.example.content.activity

import android.content.Intent
import android.util.Log
import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.example.content.R
import com.example.content.SELECT_TYPE
import com.example.content.adapter.viewpager.HomePagerAdapter
import com.example.content.databinding.ActivityMainBinding
import com.example.content.fragment.main.CityFragment
import com.example.content.fragment.main.HomeFragment
import com.example.content.fragment.main.MeFragment
import com.example.content.fragment.main.MessageFragment
import com.example.content.manager.GlideEngine
import com.example.content.tab.HomeTab
import com.example.content.viewmodule.main.CommonHomeViewModel
import com.example.content.views.ShowProductPopup
import com.example.uiwidegt.tablayout.listener.CustomTabEntity
import com.example.uiwidegt.tablayout.listener.OnTabSelectListener
import com.huantansheng.easyphotos.EasyPhotos
import com.huantansheng.easyphotos.constant.Type
import com.huantansheng.easyphotos.models.album.entity.Photo
import com.huantansheng.easyphotos.setting.Setting

class MainActivity : BaseActivity<ActivityMainBinding, CommonHomeViewModel>() {
    private val TAG = MainActivity::class.java.simpleName
    private val entities: List<CustomTabEntity> by lazy { HomeTab.getTabEntities() }
    private var homePagerAdapter: HomePagerAdapter? = null
    private val fragmentList by lazy {
        listOf(
            HomeFragment.newInstance(),
            CityFragment.newInstance(),
            Fragment(),
            MessageFragment.newInstance(),
            MeFragment.newInstance()
        )
    }
    private lateinit var photoPopup:ShowProductPopup

    private var selectType = 2

    override fun initView() {
        photoPopup = ShowProductPopup(this)
    }

    override fun initData() {
        homePagerAdapter = HomePagerAdapter(supportFragmentManager, fragmentList, HomeTab.titles)
        //设置底部tab的信息
        dataBinding.tabLayout.setTabData(entities)
        dataBinding.viewPager.adapter = homePagerAdapter

        viewModel.loginIM()
    }

    override fun initListener() {
        dataBinding.tabLayout.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                Log.i(TAG, "onTabSelect: tab select position: $position")
                dataBinding.viewPager.currentItem = position
            }

            override fun onTabReselect(position: Int) {

            }
        })
        dataBinding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
            }

            override fun onPageSelected(position: Int) {
                dataBinding.tabLayout.currentTab = position
            }

            override fun onPageScrollStateChanged(state: Int) {
            }
        })
        dataBinding.viewPager.currentItem = 0
        val viewCLose = photoPopup.contentView.findViewById<View>(R.id.close)
        viewCLose.setOnClickListener {photoPopup.dismiss()}
        val selectVideo = photoPopup.contentView.findViewById<View>(R.id.select_video)
        selectVideo.setOnClickListener{
            selectType = 1
//            EasyPhotos.createAlbum(this, true, GlideEngine)
//                .setFileProviderAuthority("com.example.pvshow.fileprovider")
//                .setCount(1)
//                .filter(Type.VIDEO)
//                .start(REQUEST_CODE)
            startActivity(Intent(this@MainActivity, CaptureVideoActivity::class.java))
            photoPopup.dismiss()
        }
        val selectPhoto = photoPopup.contentView.findViewById<View>(R.id.select_photo)
        selectPhoto.setOnClickListener{
            selectType = 2
            EasyPhotos.createAlbum(this, true, GlideEngine)
                .setFileProviderAuthority("com.example.pvshow.fileprovider")
                .setCameraLocation(Setting.LIST_FIRST)
                .setCount(9)
                .start(REQUEST_CODE)
            photoPopup.dismiss()
        }
        dataBinding.publishNews.setOnClickListener { photoPopup.showPopupWindow() }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE && data != null) {
            val images = data.getParcelableArrayListExtra<Photo>(EasyPhotos.RESULT_PHOTOS)
            val intent = Intent(this@MainActivity, UpProductActivity::class.java)
            intent.putParcelableArrayListExtra(EasyPhotos.RESULT_PHOTOS, images)
            intent.putExtra(SELECT_TYPE, selectType)
            startActivity(intent)
        }
        for (fragment in fragmentList) {
            fragment.onActivityResult(requestCode, resultCode, data)
        }
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_main
    }

    companion object {
        const val REQUEST_CODE = 1001
    }
}